<!DOCTYPE html>
<?php
$frist=!count($_POST);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($frist){
           
        if($_GET["id"]){
         $uid=(int)$_GET["id"];
        //要求用户id必须是数字
         if($uid){
        $hostname="127.0.0.1";
        $username="root";
        $password="123456";
        $database="add";
        $connect=mysqli_connect($hostname, $username,
                $password, $database);
        //设置连接的字符集
        mysqli_set_charset($connect, "utf-8");
        if($connect){
            $sqlstring="select * from user where uid=${uid}";
            $result=mysqli_query($connect, $sqlstring);
            //mysqli_num_rows 获得查询结果集中有多少条数据 
           if( mysqli_num_rows($result)==0){
               echo "参数错误";
           }else{
               $row= mysqli_fetch_array($result);
               
               ?>
         <h1>编辑用户</h1>
        <form action="edituser.php" method="post"  enctype="multipart/form-data">
            <input type="hidden" name="uid" value="<?php echo $row["uid"];?>"/>
            账户：<input type="text" name="username" value="<?php echo $row["username"];?>"/><br/>
            密码：<input type="password" name="passwd" value="<?php echo $row["passwd"];?>"/><br/>
            性别：<select name="sex">
                <option <?php echo $row["sex"]=='男'?"selected=\"selected\"":"";?> >男</option>
                <option <?php echo $row["sex"]=='女'?"selected=\"selected\"":"";?> >女</option>
            </select>
            <br/>
            年龄：
            <select name="age">
                <?php
                for($index=18;$index<=100;$index++){
                    if($index==$row["age"]){
                        echo "<option selected=\"selected\">$index</option>";
                    }else{
                         echo "<option>$index</option>";
                    }
                    
                }
   
                ?>
            
            </select>
            <br/>
            <br/>
            <input type="file">
            <br/>
            <br/>
            <input type="submit" value="确定"/>
            <input type="reset" value="取消"/>
        </form>
        <?php
           }
        }
         }else{
             echo "参数错误";
         }
        }
            
            ?>
       
        
        <?php 
        }else{
            //页面处理数据
                 //不是第一次访问的话 就处理数据
           $name=$_POST["username"]; 
           $upwd=$_POST["passwd"];
           $age=$_POST["age"];
           $sex=$_POST["sex"];
           $user_id=$_POST["uid"];
           
        $hostname="127.0.0.1";
        $username="root";
        $password="123456";
        $database="add";
        $connect=mysqli_connect($hostname, $username, $password, $database);
        //设置连接的字符集
        mysqli_set_charset($connect, "utf-8");
        if($connect){
            $sqlstring="update user set username='${name}',passwd='${upwd}'"
            . ",sex='${sex}',age='${age}' where uid=${user_id}";
            $result=mysqli_query($connect, $sqlstring);        
            if($result){
                echo  "<script>alert('修改成功！');"
                . "location='index.php';</script>";
            }else{
                echo "添加失败";
            }
        }
        mysqli_close($connect);
        }
        ?>
    </body>
</html>
